<template>
  <div>
    <!-- 这里要记住不能v-for绑定el-row -->
    <div v-for="(item, index) in lands" :key="item.landId" class="land">
      <!-- 第一条信息 -->
      <el-row>
        <!-- 土地部分 -->
        <el-col :span="6" class="landName">
          <img :src="item.landPic" class="landPic" />
          <span>{{ item.landName }}</span>
        </el-col>
        <!-- 文字部分  -->
        <el-col :span="6" class="text2">
          <h1>位置:{{ item.position }}</h1>
        </el-col>
      </el-row>
    </div>
    <!-- 新的土地 -->
    <div class="newLand">
      <h1>没有更多土地了</h1>
      <el-button text @click="dialogVisibleLand = true"> 新的土地 </el-button>
    </div>
    <!-- 新的土地 -->
    <!-- 筛选部分 -->
    <el-dialog
      class="addNewLand"
      v-model="dialogVisibleLand"
      width="32%"
      :before-close="handleClose"
      style="background-color: #a3ea9b"
    >
      <!-- 筛选界面 -->
      <div class="inNewLand">
        <!-- 筛选标题 -->
        <h1 class="title">
          接下来通过筛选
          <br />
          我们将为您选出合适的土地
        </h1>
        <!-- 筛选作物 -->
        <h1>钟意的作物</h1>
        <el-select v-model="plant" placeholder="Select" style="width: 240px">
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <!-- 筛选土地 -->
        <h1>土地位置</h1>
        <el-select v-model="position" placeholder="Select" style="width: 240px">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <div>
          <!-- 筛选确认按钮 -->
          <el-button @click="filter()" style="margin-bottom: 15px">
            确定
          </el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 筛选完成后界面 -->
    <el-dialog
      class=""
      v-model="dialogVisibleFilter"
      width="42%"
      :before-close="handleClose"
      style="background-color: #a3ea9b"
      title="空闲土地"
    >
      <!-- 对筛选完的数据循环 -->
      <div
        v-for="(item, index) in filterLands"
        :key="item.landId"
        class="filterLand"
      >
        <!-- 筛选后土地图片 -->
        <div>
          <img :src="item.landPic" />
        </div>
        <!-- 筛选后土地功能 -->
        <div>
          <h1>{{ item.landName }}</h1>
          <el-button @click="buyLand(item)">认购*</el-button>
          <div></div>
          <el-button>拼单</el-button>
        </div>
        <!-- 筛选后土地文字信息 -->
        <div>
          <h1>状态:{{ item.landStatus }}</h1>
          <h1>耕作者:{{ item.farmer }}</h1>
          <h1>位置:{{ item.position }}</h1>
        </div>
      </div>
      <span>*认购指认购土地上的农产品</span>
      <!-- 这里要记住不能v-for绑定el-row -->
    </el-dialog>
  </div>
</template>

<script>
import { ref } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";

export default {
  name: "",
  components: {},
  data() {
    return {
      // 筛选界面展示
      dialogVisibleLand: ref(false),
      // 筛选完页面展示
      dialogVisibleFilter: ref(false),
      showStatusBar: false,
      status: [false, false],
      drawStatus: true,
      show: ref(true),
      //筛选条件
      plant: ref(""),
      position: ref(""),

      //   已有土地
      lands: [
        {
          landId: 1,
          landName: "土地A",
          landPic: require("@/assets/backgroundImage.png"),
          position: "田间",
        },
        {
          landId: 2,
          landName: "土地B",
          landPic: require("@/assets/backgroundImage.png"),
          position: "山里",
        },
      ],
      // 所有空闲土地
      vacantLands: [
        {
          landId: 3,
          landName: "土地C",
          plant: "土豆",
          landPic: require("@/assets/backgroundImage.png"),
          farmer: "小黄一家",
          landStatus: "闲置",
          position: "东区1号",
        },
        {
          landId: 4,
          landName: "土地D",
          plant: "白菜",
          landPic: require("@/assets/backgroundImage.png"),
          farmer: "小白一家",
          landStatus: "闲置",
          position: "东区2号",
        },
        {
          landId: 5,
          landName: "土地E",
          plant: "苹果",
          landPic: require("@/assets/backgroundImage.png"),
          farmer: "小梁一家",
          landStatus: "闲置",
          position: "西区1号",
        },
        {
          landId: 6,
          landName: "土地F",
          plant: "梨子",
          landPic: require("@/assets/backgroundImage.png"),
          farmer: "小倩一家",
          landStatus: "闲置",
          position: "西区2号",
        },
      ],
      //   筛选后的土地
      filterLands: [
        // {
        // landId: 3,
        // landName: "土地A",
        // landPic: require("@/assets/backgroundImage.png"),
        // owner: "",
        // landStatus: "闲置",
        // farmer: "小黄一家",
        // position: "田间",landId: 3,
        // },
        {
          landId: 0,
          landName: "",
          landPic: "",
          owner: "",
          landStatus: "",
          farmer: "",
          position: "",
        },
      ],
      options1: [
        {
          value: "土豆",
          label: "土豆",
        },
        {
          value: "小麦",
          label: "小麦",
        },
        {
          value: "红薯",
          label: "红薯",
        },
        {
          value: "萝卜",
          label: "萝卜",
        },
        {
          value: "白菜",
          label: "白菜",
        },
        {
          value: "苹果",
          label: "苹果",
        },
        {
          value: "西红柿",
          label: "西红柿",
        },
        {
          value: "梨子",
          label: "梨子",
        },
        {
          value: "玉米",
          label: "玉米",
        },
        {
          value: "大豆",
          label: "大豆",
        },
      ],
      options2: [
        {
          value: "东区1号",
          label: "东区1号",
        },
        {
          value: "东区2号",
          label: "东区2号",
        },
        {
          value: "东区3号",
          label: "东区3号",
        },
        {
          value: "西区1号",
          label: "西区1号",
        },
        {
          value: "西区2号",
          label: "西区2号",
        },
        {
          value: "西区3号",
          label: "西区3号",
        },
      ],
    };
  },
  methods: {
    buyLand(item) {
      const land = {};

      //认购土地
      land.landId = item.landId;
      land.landName = item.landName;
      land.plant = item.plant;
      land.landPic = item.landPic;
      land.farmer = item.farmer;
      land.status = item.landStatus;
      land.position = item.position;
      // console.log(land.join(","));
      this.lands.push(land);
      for (let i = 0; i < this.vacantLands.length; i++) {
        if (land.landId == this.vacantLands[i].landId) {
          this.vacantLands.splice(i);
        }
      }
      this.dialogVisibleFilter = false;
      ElMessage({
        message: "认购成功！",
        type: "success",
      });
    },
    // 筛选土地
    filter() {
      let judge = 0;
      if (this.plant != "" && this.position != "") {
        this.filterLands = [];
        //如果有符合的話
        for (let i = 0; i < this.vacantLands.length; i++) {
          if (
            this.plant == this.vacantLands[i].plant &&
            this.position == this.vacantLands[i].position
          ) {
            const land = {};
            land.landId = this.vacantLands[i].landId;
            land.landName = this.vacantLands[i].landName;
            land.landPic = this.vacantLands[i].landPic;
            land.landStatus = this.vacantLands[i].landStatus;
            land.farmer = this.vacantLands[i].farmer;
            land.position = this.vacantLands[i].position;
            this.filterLands.push(land);
            this.dialogVisibleFilter = true;
            this.dialogVisibleLand = false;
            judge = 1;
            ElMessage({
              message: "筛选成功！",
              type: "success",
            });
          }
        }
        if (judge == 0) {
          ElMessage({
            message: "当前条件下未发现有符合的空闲土地！",
            type: "warning",
          });
        }
      } else {
        if (this.plant == "") {
          ElMessage({
            message: "请选农作物类型！",
            type: "error",
          });
        } else if (this.position == "") {
          ElMessage({
            message: "请选择土地位置！",
            type: "error",
          });
        }
      }
    },
  },
  //从后端接口获取数据
  created() {
    // this.getData();
    // this.setStatus();
  },
  mounted() {},
};
</script>
<style lang="css" scoped>
.transition-box {
  margin-bottom: 10px;
  width: 200px;
  height: 100px;
  border-radius: 4px;
  background-color: red;
  text-align: center;
  color: #fff;
  padding: 40px 20px;
  box-sizing: border-box;
  margin-right: 20px;
}

/* 土地部分 */
.land {
  border: solid 0.5px rgb(0, 0, 0);
  /* background-color: gold; */
  /* #67b383 */
  border-collapse: collapse;
}

.landName {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  margin-left: 5px;
}

.landName span {
  padding-left: 20px;
}

.landPic {
  height: 100px;
  width: 150px;
  margin: 10px 0 10px 10px;
  /* margin: 20px 0 0 190px; */
}

/* 文字部分 */
.text2 h1 {
  padding-left: 15px;
  font-size: 25px;
  font-weight: normal;
  margin-block-start: 0;
  margin-block-end: 0;
}

.text1 {
  display: flex;
  justify-content: space-between;
  flex-direction: column;
}

.text2 {
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  margin: auto 0 auto 10px;
}

/* 创建新的土地部分 */
.newLand {
  display: flex;
  justify-content: center;
  align-items: center;

  height: 100px;
  font-size: 30px;
}

.newLand h1 {
  font-weight: 100;
}

.newLand .el-button {
  background-color: #4bc05d;
  color: white;
  width: 150px;
  height: 50px;
  margin-left: 10px;
  font-size: 20px;
}

/* 创建新的土地部分浮窗 */
.addNewLand .el-dialog__body {
  padding: 0;
}

.inNewLand {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  color: white;
}

.inNewLand .title {
  font-size: 25px;
}

.inNewLand h1 {
  margin: 10px 0;
  font-weight: 100;
  font-size: 30px;
}

.inNewLand .el-button {
  background-color: #4bbf5c;
  color: white;
  height: 70px;
  width: 200px;
  margin-top: 10px;
  font-size: 45px;
  font-weight: 100;
}

/* 筛选部分 */
.filterLand {
  display: flex;
  justify-content: space-around;
  border-bottom: black 1px solid;
}

.filterLand h1 {
  color: white;
  font-size: 28px;
  font-weight: 300;
  margin: 0;
  padding: 0;
}
.filterLand div:nth-child(1) img {
  margin-top: 15px;
  height: 100px;
  width: 100px;
}

.filterLand div:nth-child(2) {
  display: flex;
  flex-direction: column;
}

.filterLand .el-button {
  background-color: #4bbf5c;
  color: white;
  font-size: 22px;
  height: 40px;
  width: 100%;
}
</style>
